﻿#pragma once
#ifndef SerialInBuffer_hpp
#define SerialInBuffer_hpp

#include<vector>
#include<string>
#include<string_view>
#include<stdint.h>

namespace MMFSerialize{

    //用于进行序列化的基本类型
    //只会提供基本类型的序列化
    //内部会有扩容，使用使要注意，建议搭配对象池使用
    struct SerialInBuffer
    {
    
        std::string_view mBuff;        ///< 内部数据
        uint32_t mPos;                  ///< 当前的位置

        SerialInBuffer(char* data, uint32_t size, uint32_t startIdx = 0u);
        SerialInBuffer(const std::vector<char>& data, uint32_t startIdx = 0u);
        ~SerialInBuffer();

        //将数据放入到缓存中
        void Push(bool val);
        void Push(int8_t val);
        void Push(uint8_t val);
        void Push(int16_t val);
        void Push(uint16_t val);
        void Push(int32_t val);
        void Push(uint32_t val);
        void Push(int64_t val);
        void Push(uint64_t val);
        void Push(float val);
        void Push(double val);
        void Push(const std::string& val);
        void Push(const std::string_view val);
    };

    //全局运算重载，方便使用
    SerialInBuffer& operator>>(SerialInBuffer& buff, bool val);
    SerialInBuffer& operator>>(SerialInBuffer& buff, int8_t val);
    SerialInBuffer& operator>>(SerialInBuffer& buff, uint8_t val);
    SerialInBuffer& operator>>(SerialInBuffer& buff, int16_t val);
    SerialInBuffer& operator>>(SerialInBuffer& buff, uint16_t val);
    SerialInBuffer& operator>>(SerialInBuffer& buff, int32_t val);
    SerialInBuffer& operator>>(SerialInBuffer& buff, uint32_t val);
    SerialInBuffer& operator>>(SerialInBuffer& buff, int64_t val);
    SerialInBuffer& operator>>(SerialInBuffer& buff, uint64_t val);
    SerialInBuffer& operator>>(SerialInBuffer& buff, float val);
    SerialInBuffer& operator>>(SerialInBuffer& buff, double val);
    SerialInBuffer& operator>>(SerialInBuffer& buff, const std::string& val);
    SerialInBuffer& operator>>(SerialInBuffer& buff, const std::string_view& val);

    struct SerialOutBuffer
    {
    
        std::string_view mBuff;             ///< 内部数据
        uint32_t mPos;                      ///< 当前的位置

        SerialOutBuffer(char* buff, uint32_t buffSize);
        SerialOutBuffer(const std::string_view& buff);
        ~SerialOutBuffer();

        //将数据放入到缓存中
        void Pop(bool& val);
        void Pop(int8_t& val);
        void Pop(uint8_t& val);
        void Pop(int16_t& val);
        void Pop(uint16_t& val);
        void Pop(int32_t& val);
        void Pop(uint32_t& val);
        void Pop(int64_t& val);
        void Pop(uint64_t& val);
        void Pop(float& val);
        void Pop(double& val);
        void Pop(std::string& val);
        void Pop(std::string_view& val);
    };

    //全局运算重载，方便使用
    SerialOutBuffer& operator<<(SerialOutBuffer& buff, bool& val);
    SerialOutBuffer& operator<<(SerialOutBuffer& buff, int8_t& val);
    SerialOutBuffer& operator<<(SerialOutBuffer& buff, uint8_t& val);
    SerialOutBuffer& operator<<(SerialOutBuffer& buff, int16_t& val);
    SerialOutBuffer& operator<<(SerialOutBuffer& buff, uint16_t& val);
    SerialOutBuffer& operator<<(SerialOutBuffer& buff, int32_t& val);
    SerialOutBuffer& operator<<(SerialOutBuffer& buff, uint32_t& val);
    SerialOutBuffer& operator<<(SerialOutBuffer& buff, int64_t& val);
    SerialOutBuffer& operator<<(SerialOutBuffer& buff, uint64_t& val);
    SerialOutBuffer& operator<<(SerialOutBuffer& buff, float& val);
    SerialOutBuffer& operator<<(SerialOutBuffer& buff, double& val);
    SerialOutBuffer& operator<<(SerialOutBuffer& buff, std::string& val);
    SerialOutBuffer& operator<<(SerialOutBuffer& buff, std::string_view& val);
}

#endif